﻿#ifndef __SORTTOOL__
#define __SORTTOOL__
#define _CRT_RAND_S

#include <stdlib.h>
#include <assert.h>
#include <math.h>


#define MAX_ARRAY_NUM 100
static int g_array[MAX_ARRAY_NUM];

template <class item>
void exchange(item& left, item& right){
	item temp(left); left = right; right = temp;
}

template <class item>
void compare_and_exchange(item& left, item& right){
	if(left > right)
		exchange(left, right);
}

unsigned int get_random_uint(){
	unsigned int num;
	assert(rand_s(&num) == 0);
	return num;
}

int get_random_int_from(int min, int max){
	if(min == max) return min;
	assert(max > min);
	unsigned int rand = get_random_uint();
	if(rand % 2)
		return 	(int)floor( (double)rand / UINT_MAX * (max - min) ) + min;
	else
		return (int)ceil( (double)rand / UINT_MAX * (max - min) ) + min;
}

void initialize_global_array(){
	for(int i = 0; i < MAX_ARRAY_NUM; ++i){
		g_array[i] = get_random_int_from(0, MAX_ARRAY_NUM);
	}
}

void print_global_array(){
	for(int i = 0; i < MAX_ARRAY_NUM; ++i){
		printf("%4d\t ",g_array[i]);
	}
	printf("\n");
}

const int BITSWORD = 32;
const int BITSBYTE = 8;
const int WORDBYTES = BITSWORD / BITSBYTE;
const int RADIX = 1 << BITSBYTE;

//指定提取一个数的字节
int getbyte(long num, int digit){
	assert(digit >= 0 && digit <= 3);
	return (num >> BITSBYTE *(WORDBYTES - digit -1)) & (RADIX - 1);
}
//指定提取一个数的位
int getbit(long num, int bit){
	assert(bit >= 0 && bit <= 31); 
	return	(num &1<<(BITSWORD-1-bit)) >>(BITSWORD-1 - bit);
}
#endif